<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class TestController extends Controller
{
    //

    /**
     * 分割函数，string implode ( string $glue , array $pieces )
     * 将数组转换成字符串
     */
    public function testImplode()
    {
        $array = array('lastname', 'email', 'phone');
        $comma_separated = implode(",", $array);

        echo $comma_separated; // lastname,email,phone

        // Empty string when using an empty array:
        var_dump(implode($array)); // string(0) ""
    }

    /**
     * array explode ( string $delimiter , string $string [, int $limit ] )
     * 将字符串转换成数组
     */
    public function  testExplode()
    {
        //example 1
        $pizza = "piece1 piece2 piece3 piece4 piece5 piece6";
        $pieces = explode(" ", $pizza);
        dump($pieces);
        dump($pieces[0]); // piece1
        dump($pieces[1]); // piece2

        //example 2
        $data = "foo:*:1023:1000::/home/foo:/bin/sh";
        list($user, $pass, $uid, $gid, $gecos, $home, $shell) = explode(":", $data);
        dump($data);
        dump($user); // foo
        dump($pass); // *
        dump($uid);
        dump($gid);
        dump($gecos);
        dump($home);
        dump($shell);

        //example 3
        $input1 = "hello";
        $input2 = "hello,there";
        dump(explode(',', $input1));
        dump(explode(',', $input2));
    }

    /**
     * int preg_match ( string $pattern , string $subject [,array
     * &$matches [, int $flags = 0 [, int $offset = 0 ]]] )
     * 正则、需匹配的字符，匹配结果，偏移量
     */
    public function  testPregMatch()
    {

        //example 1
        $subject = "abcdef";
        $pattern = '/^def/';
        preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE, 3);
        print_r($matches);

        //example 2
        $name = "/www/app/app/Http/Controllers/Business/Mobile/Api/AppController.php";
        preg_match('/\w*Controller\.php/', $name, $start, PREG_OFFSET_CAPTURE);//匹配 *Controller.php起始位置
        preg_match('/Controller\.php/', $name, $end, PREG_OFFSET_CAPTURE);//匹配 *Controller.php结束位置
        dump($name);
        dump($start);
        dump($end);
        $result = substr($name, $start[0][1], $end[0][1] - $start[0][1]);
        dump($result);
    }


    /**
     * 通过一个正则表达式分隔字符串
     * array preg_split ( string $pattern ,
     * string $subject [, int $limit = -1 [, int $flags = 0 ]] )
     */
    public function testPreg_split()
    {
        $one = preg_split("/[\s,]+/", "hypertext language, programming");
        $two = preg_split("/[\s,]+/", "hypertext language, programming", -1);
        $three = preg_split("/[\s,]+/", "hypertext language, programming", -1, PREG_SPLIT_NO_EMPTY);
        $four = preg_split("/[\s,]+/", "(hypertext) (language), (programming)", -1, PREG_SPLIT_DELIM_CAPTURE);
        $five = preg_split("/[\s,]+/", "hypertext language, programming", -1, PREG_SPLIT_OFFSET_CAPTURE);
        dump($one);
        dump($two);
        dump($three);
        dump($four);
        dump($five);
    }

    /**
     * 将字符串解析成多个变量
     */
    public function testParse_str()
    {
        $str = "first=value&arr[]=foo+bar&arr[]=baz";
        parse_str($str);
        dump($str);  // null
        dump($str[0]); // f
        dump($str[10]); // e

        parse_str($str, $output);
        foreach ($output as $key) {
            dump($key);
        }

        dump($output['first']);  // value
        dump($output['arr'][0]); // foo bar
        dump($output['arr'][1]); // baz
    }

    /**
     * 返回字符串的子串
     * string substr ( string $string , int $start [, int $length ] )
     */
    public function testSubstr()
    {
        $one = substr("abcdef", 0, -1);  // 返回 "abcde"
        $two = substr("abcdef", 2, -1);  // 返回 "cde"
        $three = substr("abcdef", 4, -4);  // 返回 "false"
        $four = substr("abcdef", -3, -1); // 返回 "de"
        dump($one);
        dump($two);
        dump($three);
        dump($four);
    }

    /**
     * 去除字符串首尾处的空白字符（或者其他字符）
     * string trim ( string $str [, string $charlist = " \t\n\r\0\x0B" ] )
     * " "普通空格符
     * \t 制表符
     * \n 换行符
     * \r 回车符
     * \0 空字节符
     * \x0B 垂直制表符
     */
    public function testTrim()
    {
        $text = "\t\tThese are a few words :) ...  ";
        $binary = "\x09Example string\x0A";
        $hello = "Hello World";

        dump($text);
        dump($binary);
        dump($hello);

        $one = trim($text);
        dump($one);

        $two = trim($text, " \t.");
        dump($two);

        $three = trim($hello, "o WH");
        dump($three);

// 清除 $binary 首位的 ASCII 控制字符
// （包括 0-31）
        $clean = trim($binary, "\x00..\x1F");
        var_dump($clean);
    }

    /**
     * 查找字符串首次出现的位置
     * mixed strpos ( string $haystack , mixed $needle [, int $offset = 0 ] )
     */
    public function testStrops()
    {

        $mystring = 'abc';
        $findme = 'c';
        $pos = strpos($mystring, $findme);

        dump($pos);
// 注意这里使用的是 ===。简单的 == 不能像我们期待的那样工作，
// 因为 'a' 是第 0 位置上的（第一个）字符。
        if ($pos === false) {
            echo "The string '$findme' was not found in the string '$mystring'";
        }
        if ($pos !== false) {
            echo "The string '$findme' was found in the string '$mystring'";
            echo " and exists at position $pos";
        }

        // 忽视位置偏移量之前的字符进行查找
        $newstring = 'abcdef abcdef';
        $pos = strpos($newstring, 'a', 1); // $pos = 7, 不是 0
        dump($pos);
    }

    /*
     * 获取字符串长度
     */
    public function testStrlen()
    {
        $str = 'abcdef';
        echo strlen($str);

    }

}
